package com.example.teesystem.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CustomMapContainsValidator implements ConstraintValidator<MapContains, Map<String, Integer>> {
    private List<String> keys = new ArrayList<>();
    private int minValue;
    
    @Override
    public void initialize(MapContains constraintAnnotation) {
        this.keys.addAll(Arrays.asList(constraintAnnotation.keys()));
        this.minValue = constraintAnnotation.minValue();
    }
    
    @Override
    public boolean isValid(Map<String, Integer> value, ConstraintValidatorContext context) {
        if(value == null) {
            return false;
        }
        for(String key : keys) {
            if(!value.containsKey(key) || value.get(key) == null || value.get(key) < minValue) {
                return false;
            }
        }
        return true;
    }
}